package hai.nguyenvan.service;

import javax.swing.JOptionPane;

public class Validation {

	public static boolean isIPAddress(String address){
		boolean check = true;
		
		String[] octects = new String[address.length()];
		int index = address.indexOf(".");
		int count = 0;
		
		while (index != -1) {
			octects[count] = address.substring(0, index).trim();
			count++;
			address = address.substring(index + 1);
			index = address.indexOf(".");
		}
		
		octects[count] = address.trim();
		if (count != 3) {
			return false;
		} else {
			int octet = 0;
			int count0 = 0;
			int count255 = 0;
			
			try {
				for (int i = 0; i <= count ; i++) {
					octet = Integer.parseInt(octects[i]);
					if (octet < 0 || octet > 255) {
						return false;
					}
					
					if (octet == 0) {
						count0++;
					}
					
					if (octet == 255) {
						count255++;
					}
				}
				
				if (count0 == 4 || count255 == 4) {
					return false;
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				JOptionPane.showMessageDialog(null, "Error " + e.toString());
				return false;
			}
		}
		
		return check;
	}
}
